package com.yelobblog.yelobblog.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yelobblog.yelobblog.domain.Vo.dialogueGroupVo;
import com.yelobblog.yelobblog.domain.entity.agent;
import com.yelobblog.yelobblog.domain.entity.dialogue;
import com.yelobblog.yelobblog.domain.entity.dialogueGroup;
import org.apache.ibatis.annotations.*;

@Mapper
public interface agentMapper {

    @Select("SELECT * FROM agent WHERE status= #{status} ORDER BY createTime DESC")
    Page<agent> getAgentList(Page<?> page,Integer status);

    @Select("SELECT * FROM agent WHERE id = #{id}")
    agent getAgentById(Integer id);

    @Insert("INSERT INTO agent (agentName,agentOpening,agentDesc, userId,status) VALUES (#{agentName}, #{agentOpening}, #{agentDesc}, #{userId}, #{status})")
    void addAgent(agent agent);

    @Update("UPDATE agent " +
            "SET agentName = #{agentName}, " +
            "agentOpening = #{agentOpening}, " +
            "agentDesc = #{agentDesc}, " +
            "status = #{status}, " +
            "agentAvatar = #{agentAvatar}, " +
            "agentBackground = #{agentBackground} WHERE id = #{id}")
    void updateAgent(agent agent);



    @Select("SELECT * FROM agent WHERE userId = #{userId}")
    Page<agent> getAgentByUserId(Page<?> page,Integer userId);

    @Select("SELECT agentId FROM dialogueGroup WHERE id = #{id}")
    Integer getAgentIdByGroupId(Integer id);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "agent", column = "agentId",
                    one = @One(select = "com.yelobblog.yelobblog.mapper.agentMapper.getAgentById"))
    })
    @Select("SELECT * FROM dialogueGroup WHERE agentId = #{agentId} AND userId = #{userId}")
    Page<dialogueGroupVo> getDialogueGroupByAgentId(Page<?> page, Integer agentId, Integer userId);

    @Select("SELECT userId FROM dialogueGroup WHERE id = #{id}")
    Integer getUserIdOfDialogueGroupById(Integer id);

    @Insert("INSERT INTO dialogueGroup (name, userId, agentId) VALUES (#{name}, #{userId}, #{agentId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer addDialogueGroup(dialogueGroup dialogueGroup);

    @Delete("DELETE FROM dialogueGroup WHERE id = #{id}")
    void deleteDialogueGroup(Integer id);


    @Select("SELECT * FROM dialogue WHERE groupId = #{groupId} ORDER BY createTime DESC")
    Page<dialogue> getDialogueByGroupId(Page<?> page, Integer groupId);

    @Insert("INSERT INTO dialogue (content, userId, groupId) VALUES (#{content}, #{userId}, #{groupId})")
    void addDialogue(String content, Integer userId, Integer groupId);
}
